const  { connection }  = require('../../../model/database')
const bcrypt = require('bcrypt')
module.exports =  (req, res) => {
    // 获取post传递过来的值
    var user = req.body
    var sql = "select * from user where id  = '1'"
    // 发送
    connection.query(sql, async (error, reslut) => {
        // 失败
        if (error) {
            console.log('失败');
            return
        }
        // 判断前端传递过来的密码是否正确
        var isValid = await bcrypt.compare(user.password,reslut[0].password)
        // 判断是否成功登陆的初始值
        var isstate = false
        req.session.username = isstate
        // 判断账号密码是否正确
        if (reslut[0].user == user.name && isValid) {
            // 登陆成功
            // 将用户名存储在请求对象中
            isstate = true
            // 将登陆成功值 存储到 req中
            req.session.username = isstate
            res.status(200).send({
                message: '登陆成功',
                id:reslut[0].id,
                name: reslut[0].uname
                
            })
        } else {
            isstate = false
            req.session.username = isstate
            res.status(400).send({
                message: '登陆失败',
            })
        }
    })
 
}     